package vip.indo.controller;

import lombok.val;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import vip.indo.common.JsonResult;
import vip.indo.entity.User;
import vip.indo.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: tianlianyin
 * @Date: 2024/11/27 14:54
 * @Descripition:
 */
@RestController//标识为控制器
@RequestMapping("/user")//路径
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 模拟查询用户信息
     *
     * @return
     */
    @GetMapping
    public JsonResult getUser() {
        return JsonResult.success(User.builder().id(1L).username("tianlianyin").password("123456").build());
    }

    /**
     * 模拟查询用户列表
     *
     * @return
     */
    @GetMapping("/list")
    public JsonResult getUserList() {
        List<User> userList = new ArrayList<>();
        User user1 = User.builder().id(1L).username("tianlianyin").password("123456").build();
        User user2 = User.builder().id(2L).username("tianlianyin2").password("123456").build();
        userList.add(user1);
        userList.add(user2);
        return JsonResult.success(userList);
    }

    /**
     * 模拟查询用户map
     *
     * @return
     */
    @GetMapping("/map")
    public JsonResult getMap() {
        Map<String, Object> map = new HashMap<>(3);
        User user = User.builder().id(1L).username("tianlianyin").password("123456").build();
        map.put("作者信息", user);
        map.put("博客地址", "http://www.tianlianyin.cn");
//        map.put("公众号","tianlianyin");
        map.put("公众号", null);
        return JsonResult.success(map);
    }

    @GetMapping("/{idd}/{name}")
    public JsonResult testPathVariable(@PathVariable("idd") Integer id, @PathVariable("name") String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("获取得id", id);
        map.put("获取得name", name);
        return JsonResult.success(map);
    }

    /**
     * 模拟请求参数
     *
     * @param id
     * @return
     */
    @GetMapping("/requestParam")
    public JsonResult testRequestParam(@RequestParam(value = "id", required = false) Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("获取得id", id);
        return JsonResult.success(map);
    }

    /**
     * 模拟请求参数
     *
     * @param username 用户名
     * @param password 密码
     * @return json串
     */
    @PostMapping("/form")
    public JsonResult testForm(@RequestParam String username, @RequestParam String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        return JsonResult.success(map);
    }

    /**
     * 模拟请求参数，封装到对象中
     *
     * @param user 对象
     * @return
     */
    @PostMapping("/formUser")
    public JsonResult testFormUser(@RequestBody User user) {
        return JsonResult.success(user);
    }

    /**
     * 获取用户信息
     *
     * @param username
     * @return
     */
    @GetMapping("/getUserByName/{username}")
    public JsonResult getUserByName(@PathVariable String username) {
        User userByName = userService.getUserByName(username);
        return JsonResult.success(userByName);
    }

    /**
     * 插入用户
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/addUser")
    public JsonResult addUser(@RequestBody User user) throws Exception {
        if (!ObjectUtils.isEmpty(user)) {
            userService.insertUser(user);
            return JsonResult.success();
        } else return JsonResult.error("用户信息为空");
    }
}
